package com.youlai.sale.controller;

import com.youlai.eng.query.BaseQuery;
import com.youlai.eng.query.PageQuery;
import com.youlai.sale.entity.SaleItemForecastEntity;
import com.youlai.sale.service.SaleItemForecastService;
import com.youlai.common.model.Option;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.utils.IdWorker;
import com.youlai.common.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "工程员工管理接口(jpa例子)")
@RestController
@RequestMapping("/api/v1/saleitemforecast")
@RequiredArgsConstructor
public class SaleItemForecastController {
    private final SaleItemForecastService service;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    public PageResult<SaleItemForecastEntity> getPage(@ParameterObject PageQuery queryParams) {
        PageResult<SaleItemForecastEntity> page = service.getPageByHql(queryParams);
        return page;
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}/form")
    public Result<SaleItemForecastEntity> getForm(
            @Parameter(description ="字典ID") @PathVariable Long id
    ) {
        SaleItemForecastEntity sysOrg = service.findById(id);
        return Result.success(sysOrg);
    }

    @Operation(summary = "新增")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sale:saleitemforecast:add')")
    @PreventDuplicateSubmit
    public Result insert(@RequestBody SaleItemForecastEntity form) {
        form.setSaleItemFcId(IdWorker.generateId());
        service.save(form);
        return Result.judge(true);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sale:saleitemforecast:edit')")
    public Result update(@PathVariable Long id, @RequestBody SaleItemForecastEntity form) {
        form.setSaleItemFcId(id);
        service.update(form);
        return Result.judge(true);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sale:saleitemforecast:del')")
    public Result deleteByIds(@Parameter(description ="主键ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = service.deleteAllById(ids);
        return Result.judge(result);
    }

    @Operation(summary = "销售客户下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listSaleCustomerOptions(@ParameterObject BaseQuery queryParams) {
        List<Option> list = service.listEngEmpOptions(queryParams);
        return Result.success(list);
    }
}
